package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ExtMyBatis3FormattingUtilities;
import org.mybatis.generator.internal.util.StringUtility;

/*********************************************************
 * 文件名称：SelectByExampleWithPageElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements
 * 功能说明：
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/24 15:48
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SelectByExampleWithPageElementGenerator extends AbstractXmlElementGenerator {
    public SelectByExampleWithPageElementGenerator() {
    }

    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("select");
        answer.addAttribute(new Attribute("id", ExtMyBatis3FormattingUtilities.ATTR_SELECT_BY_EXAMPLE_WITH_PAGE_STATEMENT_ID));
        answer.addAttribute(new Attribute("resultMap", this.introspectedTable.getBaseResultMapId()));
        this.context.getCommentGenerator().addComment(answer);
        answer.addElement(new TextElement("select"));
        XmlElement ifElement = new XmlElement("if");
        ifElement.addAttribute(new Attribute("test", "example != null and example.distinct"));
        ifElement.addElement(new TextElement("distinct"));
        answer.addElement(ifElement);
        StringBuilder sb = new StringBuilder();
        if (StringUtility.stringHasValue(this.introspectedTable.getSelectByExampleQueryId())) {
            sb.append('\'');
            sb.append(this.introspectedTable.getSelectByExampleQueryId());
            sb.append("' as QUERYID,");
            answer.addElement(new TextElement(sb.toString()));
        }

        answer.addElement(this.getBaseColumnListElement());
        sb.setLength(0);
        sb.append("from ");
        sb.append(this.introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
        answer.addElement(new TextElement(sb.toString()));
        answer.addElement(this.getExampleIncludeElement());
        ifElement = new XmlElement("if");
        ifElement.addAttribute(new Attribute("test", "example != null and example.orderByClause != null"));
        ifElement.addElement(new TextElement("order by ${example.orderByClause}"));
        answer.addElement(ifElement);
        if (this.context.getPlugins().sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer, this.introspectedTable)) {
            parentElement.addElement(answer);
        }

    }

    protected XmlElement getExampleIncludeElement() {
        XmlElement ifElement = new XmlElement("if");
        ifElement.addAttribute(new Attribute("test", "example != null"));
        XmlElement includeElement = new XmlElement("include");
        includeElement.addAttribute(new Attribute("refid", this.introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
        ifElement.addElement(includeElement);
        return ifElement;
    }
}
